﻿using Mail_Box_Net_Disk.PublicTools;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace Mail_Box_Net_Disk
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class Settings : Page
    {
        public Settings()
        {
            this.InitializeComponent();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            IMAPServerTextBox.Text = smtp.IMAPServer;
            IMAPServerPortTextBox.Text = smtp.IMAPPort.ToString();
            SMTPServerTextBox.Text = smtp.SMTPServer;
            SMTPServerPortTextBox.Text = smtp.SMTPPort.ToString();
            AccountTextBox.Text = smtp.account;
            PasswordTextBox.Password = smtp.password;
            SSLCheckBox.IsChecked = smtp.isSSL;
            if(smtp.fileSplit == 10)
            {
                File10MRadioButton.IsChecked = true;
            }
            else if(smtp.fileSplit == 30)
            {
                File30MRadioButton.IsChecked = true;
            }
            else if (smtp.fileSplit == 50)
            {
                File50MRadioButton.IsChecked = true;
            }
        }

        private async void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                FileRW.SetValue("IMAPServer", IMAPServerTextBox.Text);
                FileRW.SetValue("IMAPPort", int.Parse(IMAPServerPortTextBox.Text));
                FileRW.SetValue("SMTPServer", SMTPServerTextBox.Text);
                FileRW.SetValue("SMTPPort", int.Parse(SMTPServerPortTextBox.Text));
                FileRW.SetValue("account", AccountTextBox.Text);
                FileRW.SetValue("password", PasswordTextBox.Password);
                FileRW.SetValue("isSSL", SSLCheckBox.IsChecked);
                if (File10MRadioButton.IsChecked == true)
                {
                    FileRW.SetValue("fileSplit", 10);
                    smtp.fileSplit = 10;
                }
                else if (File30MRadioButton.IsChecked == true)
                {
                    FileRW.SetValue("fileSplit", 30);
                    smtp.fileSplit = 30;
                }
                else if (File50MRadioButton.IsChecked == true)
                {
                    FileRW.SetValue("fileSplit", 50);
                    smtp.fileSplit = 50;
                }
                smtp.IMAPServer = IMAPServerTextBox.Text;
                smtp.SMTPServer = SMTPServerTextBox.Text;
                smtp.IMAPPort = int.Parse(IMAPServerPortTextBox.Text);
                smtp.SMTPPort = int.Parse(SMTPServerPortTextBox.Text);
                smtp.account = AccountTextBox.Text;
                smtp.password = PasswordTextBox.Password;
                smtp.isSSL = (bool)SSLCheckBox.IsChecked;
                await Tools.ShowMessageDialog("提示", "设置保存成功！");
            }
            catch(Exception a)
            {
                await Tools.ShowMessageDialog("提示", "设置保存出错！请检查是否填写正确！"+a.GetBaseException().ToString());
            }
        }

        private async void SendTestMailButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                StorageFolder folder;
                folder = ApplicationData.Current.TemporaryFolder;
                StorageFile file = await folder.CreateFileAsync("TestFile.txt", CreationCollisionOption.ReplaceExisting);
                await smtp.UploadFiles(file);
                await Tools.ShowMessageDialog("提示", "邮件发送成功！");
            }
            catch (Exception a)
            {
                await Tools.ShowMessageDialog("提示", "邮件发送失败！请检查smtp设置是否填写正确！" + a.ToString());
            }

        }
    }
}
